package com.example.chess.ui;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.example.chess.R;
import com.example.chess.base.BaseActivity;
import com.example.chess.receiver.SettingReceiver;
import com.example.chess.utils.SettingSp;
import com.example.chess.widgets.CheckStateView;

public class SettingActivity extends BaseActivity {

    private CheckStateView stateView;
    private boolean originRobotEnabled;

    public static void start(Context context) {
        context.startActivity(new Intent(context, SettingActivity.class));
    }

    @Override
    protected int setLayoutResource() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        fitWithStatusBar(R.id.titleContainer);
        stateView = findViewById(R.id.stateView);
        originRobotEnabled = SettingSp.isRobotMode();
        stateView.setChecked(originRobotEnabled);
    }

    public void toggleRobotState(View view) {
        stateView.toggle();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isFinishing()) {
            boolean checked = stateView.isChecked();
            if (checked != originRobotEnabled) {
                SettingSp.setRobotMode(checked);
                SettingReceiver.sendBroadcast(this);
            }
        }
    }

    public void openSite(View view) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://www.gitee.com/xuyougen/chess"));
        startActivity(intent);
    }
}